
package net.atremmedia.gwt.actionjava.examples.bounds.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face1;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face2;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face3;
import net.atremmedia.gwt.actionjava.examples.bounds.samples.faces.Face4;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.events.tick.TickEvent;

public class DrawGraphicsBounds4 extends AJExample
{
	/*
	 * Download FLA source files:
	 * http://www.actionjava.com/fla.zip
	 */

	public static final String NAME = "Draw Graphics bounds 4";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bounds/src/main/java/net/atremmedia/gwt/actionjava/examples/bounds/samples/DrawGraphicsBounds4.java";

	private Face face;
	private Shape boundsRect;
	private Sprite faceContainer;

	private double angle = 0;
	private double centerScale = 1;
	private double range1 = .5;
	private double speed = .1;

	private double angleX = 0;
	private double angleY = 0;
	private double centerX = 200;
	private double centerY = 200;
	private double range2 = 50;
	private double xspeed = .07;
	private double yspeed = .11;

	public DrawGraphicsBounds4(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		faceContainer = new Sprite();
		addChild(faceContainer);

		face = createFace();
		faceContainer.addChild(face);

		boundsRect = new Shape();
		addChild(boundsRect);
	}

	private Face createFace()
	{
		int key = (int) getRandomNumber(1, 4);

		Face face = null;
		switch (key) {
			case 1:
				face = new Face1();
				break;

			case 2:
				face = new Face2();
				break;

			case 3:
				face = new Face3();
				break;

			case 4:
				face = new Face4();
				break;
		}

		return face;
	}

	@Override
	public void onRenderTick(TickEvent event)
	{
		face.setScaleX(centerScale + Math.sin(angle) * range1);
		face.setScaleY(centerScale + Math.sin(angle) * range1);
		angle += speed;

		face.setX(centerX + Math.sin(angleX) * range2);
		face.setY(centerY + Math.sin(angleY) * range2);
		angleX += xspeed;
		angleY += yspeed;

		drawBounds(face.getX(), face.getY(), faceContainer.getWidth(), faceContainer.getHeight(), "#FF0000");
	}

	private void drawBounds(double x, double y, double w, double h, String color)
	{
		boundsRect.getGraphics().clear().beginStroke(color).drawRect(x, y, w, h).endStroke();
	}
}
